package servlet;

import service.*;
import service.WxServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@WebServlet(name = "Servlet", value = "/Servlet")
public class Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String code=request.getParameter("code");
        String info= Authorize.getUserInfo(code,"oxHlE6JNtT2RfJw__PslJ4x9kuhg");
        response.setContentType("text/html;charset=utf-8");//设置编码格式，以防前端页面出现中文乱码
        PrintWriter printWriter = response.getWriter();//创建输出流
        printWriter.println("<h1>+"+info+"</h1>");
        request.setAttribute("msg", info);
        request.getRequestDispatcher("index.jsp").forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        response.setCharacterEncoding("utf8");
        WxServiceImpl WxServiceImpl =new WxServiceImpl();
        Map<String,String> map= WxServiceImpl.parseRequest(request.getInputStream());
        String respXML= WxServiceImpl.getRespose(map);
        PrintWriter out=response.getWriter();
        out.print(respXML);
        out.flush();
        out.close();

    }
}
